﻿using Furion.DatabaseAccessor;
using Furion.DataEncryption;
using Furion.DependencyInjection;
using Furion.FriendlyException;
using HOPU.OnlineSchool.Application.User.Dtos;
using HOPU.OnlineSchool.Core;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HOPU.OnlineSchool.Application.User.Services
{
    public class UserService : IUserService, ITransient
    {
        private readonly IRepository<AclUser> _userRepository;

        public UserService(IRepository<AclUser> userRepository)
        {
            _userRepository = userRepository;
        }
        public async Task<LoginInfoOutput> LoginAsync(LoginInfoInput input)
        {
            //获取加密后的字符串
            var password = MD5Encryption.Encrypt(input.Password);

            //根据用户名和密码查询用户
            var user = await _userRepository.Where(u => u.Username == input.UserName && u.Password == password).FirstOrDefaultAsync();

            //如果返回为空，则说明用户不存在
            if (user == null) throw Oops.Oh("用户名或密码有误");

            //返回token（临时处理，后续优化）
            return new LoginInfoOutput() { Token = "admin" };
        }
    }
}
